//
//  SSMainTabBar.swift
//  weibooo
//
//  Created by  sulcan on 16/4/8.
//  Copyright © 2016年 苏烁. All rights reserved.
//

import UIKit
class SSMainTabBar: UITabBar {
    // 定义闭包属性,传递
    var composeClosure: (() -> ())?
    
    let buttonCount = 5
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        // 宽度
        let width: CGFloat = frame.width / CGFloat(buttonCount)
        
        // frame
        let newframe: CGRect = CGRectMake(0, 0, width, frame.height)
        
        var index = 0
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subview.frame = CGRectOffset(newframe, CGFloat(index) * width, 0)
                index += index == 1 ? 2 : 1
                
            }
        }
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
    }
    
    @objc private func didClickComposeButton() {
        composeClosure?()
    }
    
    // mark: -懒加载
    // 加号按钮
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: Selector("didClickComposeButton"), forControlEvents: UIControlEvents.TouchUpInside)
        
        // 添加按钮到tabBar
        self.addSubview(button)
        
        return button
    }()
}
